package com.sicpa.common.utils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 防伪编码生成工具类
 * SecurityCodeGenerateUtils
 *
 * @author lwf
 */
public class SecurityCodeGenerateUtils {

    /**
     * 防伪编码生成方法
     * @param officeId
     * @param warehousingId
     * @return
     */
    public static String generateSecurityCode(Long officeId, Long warehousingId) {
        StringBuilder sb = new StringBuilder();
        LocalDate localDate = LocalDate.now();
        String yy = localDate.format(DateTimeFormatter.ofPattern("yy"));
        sb.append(yy);
        sb.append(String.format("%03d", officeId));
        sb.append(String.format("%06d", warehousingId));
        sb.append(RandomNumCode.getStringRandom(3));
        return sb.toString();
    }

    /**
     * wym，20201110，刘秘给的新的编码生成规则
     * 防伪编码取数规则：共18位，前12位为事务所执业证书号（总所只有8位，添加4位报备年度补齐）+6位随机码（5位数字+最后一位纯字母，字母不要使用 I 和 O ）
     * 4位报备年度即当前当前日期
     * 防伪编码生成方法
     * @param practiceCertificateNo
     * @return
     */
    public static synchronized String generateSecurityCodeNew(String practiceCertificateNo) {
        StringBuilder sb = new StringBuilder();
        LocalDate localDate = LocalDate.now();
        String yyyy = localDate.format(DateTimeFormatter.ofPattern("yyyy"));
        if(practiceCertificateNo != null && practiceCertificateNo.length() < 12){
            sb.append(practiceCertificateNo.substring(0,8));
            sb.append(yyyy);
        }else{
            sb.append(practiceCertificateNo.substring(0,12));
        }
        sb.append(RandomNumCode.generateSpecificCode(5));
        sb.append(RandomNumCode.getSpecificString(1));
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(generateSecurityCodeNew("15142568"));
        System.out.println(generateSecurityCodeNew("514585236589"));
    }

}
